/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.jjsoft.disco.gco.view;

import com.jjsoft.disco.gco.mdl.Cliente;
import com.jjsoft.disco.gco.mdl.Cobro;
import com.jjsoft.disco.gco.mdl.Detallefactura;
import com.jjsoft.disco.gco.mdl.Factura;
import com.jjsoft.disco.gco.mdl.Ingreso;
import com.jjsoft.disco.gco.ngc.CobroManager;
import com.jjsoft.disco.gco.ngc.DiarioManager;
import com.jjsoft.disco.gco.ngc.FacturaManager;
import com.jjsoft.disco.gco.ngc.IngresoManager;
import com.jjsoft.disco.gco.util.CImprimir;
import com.jjsoft.disco.gco.util.CNumeros;
import com.jjsoft.disco.gco.util.CSelecciones;
import com.jjsoft.disco.gco.util.MiModelo;
import com.jjsoft.disco.gco.util.configuraciones;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

/**
 *
 * @author Johnny
 */
public class VPrincipalCajero extends javax.swing.JFrame {

    /**
     * Creates new form VPrincipalCajero
     */
    Ingreso ingreso;
    Cliente cliente;
    Factura factura;
    MiModelo modelo = new MiModelo();
    List<Detallefactura> listaDetalle = new ArrayList();

    public VPrincipalCajero() {
        initComponents();
        txtSaldoCaja.setText(String.valueOf(configuraciones.diario.getValor()));
        String[] identificadores = {"CANT", "DESCRIPCION", "BARRA", "P. UNIT", "V. VENTA"};
        this.cmdAbrir.setVisible(false);
        this.modelo.setColumnIdentifiers(identificadores);
        this.dtgFactura.setModel(this.modelo);
        this.dtgFactura.getColumn("CANT").setPreferredWidth(30);
        this.dtgFactura.getColumn("DESCRIPCION").setPreferredWidth(330);
        this.dtgFactura.getColumn("P. UNIT").setPreferredWidth(50);
        this.dtgFactura.getColumn("V. VENTA").setPreferredWidth(50);
        this.lblLocal.setText(configuraciones.NombreLocal);
        this.lblDireccion.setText("Dirección: " + configuraciones.Direccion);
        this.lblPropietario.setText("Propietario: " + configuraciones.NombrePropietario);
        this.lblTelefono.setText("Telefono:" + configuraciones.Telefono);
        this.lblUsuario.setText("Usuario: " + configuraciones.usuario.getNombre());
        ImageIcon icono = null;
        if (configuraciones.logo != null && configuraciones.logo.length() > 0) {
            configuraciones.logo = configuraciones.logo.replace('\\', '/');
            icono = new ImageIcon(configuraciones.logo);
            this.lblLogo.setIcon(icono);
        }


    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblLocal = new javax.swing.JLabel();
        lblDireccion = new javax.swing.JLabel();
        lblTelefono = new javax.swing.JLabel();
        lblPropietario = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        txtBuscar = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtHombres = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtNombres = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        dtgFactura = new javax.swing.JTable();
        cmdCobrar = new javax.swing.JButton();
        cmdImprimirYCobrar = new javax.swing.JButton();
        txtSubtotal = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        txtSubtotalD = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        txtTotal = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        txtIVA = new javax.swing.JTextField();
        txtDescuento = new javax.swing.JSpinner();
        jLabel18 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtSaldoCaja = new javax.swing.JTextField();
        cmdCancelar = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        txtCedula = new javax.swing.JTextField();
        lblLogo = new javax.swing.JLabel();
        lblUsuario = new javax.swing.JLabel();
        cmdCerrarYSalir = new javax.swing.JButton();
        cmdTarjetasSinPagar = new javax.swing.JButton();
        cmdAbrir = new javax.swing.JButton();
        jLabel5 = new javax.swing.JLabel();
        txtConsumible = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        txtMujeres = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formKeyPressed(evt);
            }
        });

        lblLocal.setFont(new java.awt.Font("Tahoma", 0, 36)); // NOI18N
        lblLocal.setForeground(new java.awt.Color(0, 0, 255));
        lblLocal.setText("jLabel9");

        lblDireccion.setText("jLabel11");

        lblTelefono.setText("jLabel9");

        lblPropietario.setText("jLabel10");

        jLabel1.setText("Tarjeta:");

        txtBuscar.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.LOWERED));
        txtBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtBuscarActionPerformed(evt);
            }
        });
        txtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtBuscarKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtBuscarKeyReleased(evt);
            }
        });

        jLabel10.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel10.setForeground(new java.awt.Color(0, 51, 255));
        jLabel10.setText("Cliente");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(51, 51, 51));
        jLabel2.setText("Cedula/Ruc:");

        txtHombres.setEditable(false);
        txtHombres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtHombresActionPerformed(evt);
            }
        });
        txtHombres.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtHombresKeyPressed(evt);
            }
        });

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel3.setForeground(new java.awt.Color(51, 51, 51));
        jLabel3.setText("Cliente");

        txtNombres.setEditable(false);
        txtNombres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombresActionPerformed(evt);
            }
        });

        jLabel12.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel12.setForeground(new java.awt.Color(0, 51, 255));
        jLabel12.setText("Detalle de Cosumo");

        dtgFactura.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jScrollPane1.setViewportView(dtgFactura);

        cmdCobrar.setText("Cobrar");
        cmdCobrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCobrarActionPerformed(evt);
            }
        });

        cmdImprimirYCobrar.setText("Imprimir Detalle y Cobrar");
        cmdImprimirYCobrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdImprimirYCobrarActionPerformed(evt);
            }
        });

        txtSubtotal.setEditable(false);
        txtSubtotal.setEnabled(false);

        jLabel13.setText("Subtotal_0:");

        jLabel15.setText("IVA:");

        txtSubtotalD.setEditable(false);
        txtSubtotalD.setEnabled(false);

        jLabel14.setText("Subtotal_D:");

        jLabel16.setText("Descuento:");

        txtTotal.setEditable(false);
        txtTotal.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        txtTotal.setForeground(new java.awt.Color(255, 0, 0));

        jLabel17.setText("Total:");

        txtIVA.setEnabled(false);

        txtDescuento.setModel(new javax.swing.SpinnerNumberModel(0, 0, 100, 1));
        txtDescuento.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                txtDescuentoStateChanged(evt);
            }
        });

        jLabel18.setText("%");

        jLabel9.setFont(new java.awt.Font("Tahoma", 0, 18)); // NOI18N
        jLabel9.setForeground(new java.awt.Color(0, 0, 255));
        jLabel9.setText("SALDO CAJA:");

        txtSaldoCaja.setEditable(false);
        txtSaldoCaja.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        txtSaldoCaja.setForeground(new java.awt.Color(255, 0, 0));

        cmdCancelar.setText("Cancelar");
        cmdCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCancelarActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(51, 51, 51));
        jLabel4.setText("Hombres:");

        txtCedula.setEditable(false);
        txtCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCedulaActionPerformed(evt);
            }
        });
        txtCedula.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtCedulaKeyPressed(evt);
            }
        });

        cmdCerrarYSalir.setText("Cerrar Caja y salir");
        cmdCerrarYSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdCerrarYSalirActionPerformed(evt);
            }
        });

        cmdTarjetasSinPagar.setText("Ver Tarjetas sin Pagar");
        cmdTarjetasSinPagar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdTarjetasSinPagarActionPerformed(evt);
            }
        });

        cmdAbrir.setText("jButton1");
        cmdAbrir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmdAbrirActionPerformed(evt);
            }
        });

        jLabel5.setText("Consumible:");

        txtConsumible.setEditable(false);

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(51, 51, 51));
        jLabel6.setText("Mujeres:");

        txtMujeres.setEditable(false);
        txtMujeres.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtMujeresActionPerformed(evt);
            }
        });
        txtMujeres.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtMujeresKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createSequentialGroup()
                                                .addGap(29, 29, 29)
                                                .addComponent(jLabel3))
                                            .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.TRAILING))
                                        .addGap(18, 18, 18)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(txtNombres)
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(txtCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(18, 18, 18)
                                                .addComponent(jLabel4)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtHombres, javax.swing.GroupLayout.PREFERRED_SIZE, 55, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addComponent(jLabel6)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(txtMujeres, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE)))
                                        .addGap(0, 0, Short.MAX_VALUE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGap(0, 0, Short.MAX_VALUE)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(jLabel9)
                                                .addGap(18, 18, 18)
                                                .addComponent(txtSaldoCaja, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                .addComponent(jLabel12)
                                                .addGap(201, 201, 201))
                                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 572, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGroup(layout.createSequentialGroup()
                                                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                        .addGroup(layout.createSequentialGroup()
                                                            .addComponent(cmdCancelar)
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                            .addComponent(cmdCerrarYSalir)
                                                            .addGap(151, 151, 151)
                                                            .addComponent(cmdImprimirYCobrar))
                                                        .addGroup(layout.createSequentialGroup()
                                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                                                .addComponent(jLabel13)
                                                                .addComponent(jLabel15))
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                                .addGroup(layout.createSequentialGroup()
                                                                    .addComponent(txtIVA, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                    .addGap(25, 25, 25)
                                                                    .addComponent(jLabel5))
                                                                .addGroup(layout.createSequentialGroup()
                                                                    .addComponent(txtSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                    .addGap(28, 28, 28)
                                                                    .addComponent(jLabel16)))
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                                                .addComponent(txtConsumible, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(txtDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addGroup(layout.createSequentialGroup()
                                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                    .addComponent(jLabel18)
                                                                    .addGap(18, 18, 18)
                                                                    .addComponent(jLabel14))
                                                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                                                    .addGap(62, 62, 62)
                                                                    .addComponent(jLabel17)))
                                                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(txtSubtotalD, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                    .addComponent(cmdCobrar))))))
                                .addGap(40, 40, 40))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtBuscar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(10, 10, 10)
                                        .addComponent(jLabel10))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(cmdTarjetasSinPagar)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(cmdAbrir, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                .addGap(204, 204, 204))))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lblLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(55, 55, 55)
                                .addComponent(lblDireccion)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblTelefono)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lblPropietario)
                                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(lblLocal)
                                .addGap(98, 98, 98)
                                .addComponent(lblUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(19, 19, 19))))))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jSeparator1))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(17, 17, 17)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(lblLocal)
                            .addComponent(lblLogo, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lblUsuario, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblDireccion)
                    .addComponent(lblTelefono)
                    .addComponent(lblPropietario))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(txtBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel1))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmdTarjetasSinPagar, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cmdAbrir)))
                .addGap(4, 4, 4)
                .addComponent(jLabel10)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtHombres, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(txtCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtMujeres, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addGap(12, 12, 12)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(1, 1, 1)
                .addComponent(jLabel12)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 197, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(txtSubtotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel16)
                    .addComponent(txtDescuento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel18)
                    .addComponent(jLabel14)
                    .addComponent(txtSubtotalD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel15)
                    .addComponent(txtIVA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel17)
                    .addComponent(txtTotal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel5)
                    .addComponent(txtConsumible, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cmdCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmdImprimirYCobrar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cmdCobrar, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(cmdCerrarYSalir, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jLabel9, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtSaldoCaja, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(25, 25, 25))
        );

        setSize(new java.awt.Dimension(635, 706));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void txtBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtBuscarActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtBuscarActionPerformed

    private void txtBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyPressed
        // TODO add your handling code here:

        if (evt.getKeyCode() == 10) {
            ingreso = null;
            cliente = null;
            buscarTarjeta();

        } else if (evt.getKeyCode() == 27) {
            cmdCancelar.doClick();
        }
    }//GEN-LAST:event_txtBuscarKeyPressed

    private void txtBuscarKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtBuscarKeyReleased
        // TODO add your handling code here:
        //new Validacion().numeros(evt, txtBuscar);
    }//GEN-LAST:event_txtBuscarKeyReleased

    private void txtHombresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtHombresActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtHombresActionPerformed

    private void txtHombresKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtHombresKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == 10) {
//            cmdBuscar.doClick();
        }
    }//GEN-LAST:event_txtHombresKeyPressed

    private void txtNombresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombresActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNombresActionPerformed

    private void txtDescuentoStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_txtDescuentoStateChanged
        // TODO add your handling code here:
        //      Calcular();
    }//GEN-LAST:event_txtDescuentoStateChanged

    private void txtCedulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCedulaActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCedulaActionPerformed

    private void txtCedulaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtCedulaKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCedulaKeyPressed

    private void cmdCobrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCobrarActionPerformed
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(rootPane, "Esta seguro?", "Cobrar", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            CImprimir imprimir = new CImprimir();
            imprimir.imprimirConsumo(factura, configuraciones.impresoraFacturas, ingreso.getNumeroPersonasH(), ingreso.getNumeroPersonasH(), ingreso.getNumeroPersonasM());
            registrarSalida();
        }
    }//GEN-LAST:event_cmdCobrarActionPerformed

    private void cmdImprimirYCobrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdImprimirYCobrarActionPerformed
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(rootPane, "Esta seguro?", "Imprimir Detalle de Consumo y Cobrar", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            CImprimir imprimir = new CImprimir();
            imprimir.imprimirDetalleFactura(factura, configuraciones.impresoraFacturas, ingreso.getNumeroPersonasH(), ingreso.getNumeroPersonasH(), ingreso.getNumeroPersonasM());
            registrarSalida();
        }
    }//GEN-LAST:event_cmdImprimirYCobrarActionPerformed

    private void cmdCerrarYSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCerrarYSalirActionPerformed
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(rootPane, "Esta seguro?", "Cerrar caja y salir", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            List<Ingreso> listaIngresosSinPagar = new IngresoManager().buscarIngresosSinCerrar();
            if (listaIngresosSinPagar != null && listaIngresosSinPagar.size() > 0) {
                if (JOptionPane.showConfirmDialog(rootPane, "Existen Clientes que no han registrado su salida\nEsta seguro de Salir?", "Cerrar caja y salir", JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
                    return;
                }
            }
            JOptionPane.showMessageDialog(rootPane, "Se cierra caja con: " + configuraciones.diario.getValor().toString());
            new DiarioManager().cierraDiario(configuraciones.diario);
            System.exit(0);
        }
    }//GEN-LAST:event_cmdCerrarYSalirActionPerformed

    private void cmdCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdCancelarActionPerformed
        // TODO add your handling code here:
        limpiaTodo();
    }//GEN-LAST:event_cmdCancelarActionPerformed

    private void formKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_formKeyPressed

    private void cmdTarjetasSinPagarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdTarjetasSinPagarActionPerformed
        // TODO add your handling code here:
        limpiaTodo();
        new VVerTarjetasSinPagar(txtBuscar, cmdAbrir).show();
    }//GEN-LAST:event_cmdTarjetasSinPagarActionPerformed

    private void cmdAbrirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmdAbrirActionPerformed
        // TODO add your handling code here:
        ingreso = null;
        cliente = null;
        buscarTarjeta();
    }//GEN-LAST:event_cmdAbrirActionPerformed

    private void txtMujeresActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtMujeresActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMujeresActionPerformed

    private void txtMujeresKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtMujeresKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtMujeresKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(VPrincipalCajero.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(VPrincipalCajero.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(VPrincipalCajero.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(VPrincipalCajero.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VPrincipalCajero().setVisible(true);
            }
        });
    }

    private void registrarSalida() {
        Date d = new Date();
        ingreso.setPagado(factura.getTotal());
        JOptionPane.showMessageDialog(rootPane, "Total a Pagar: " + factura.getTotal());
        new IngresoManager().registrarSalida(ingreso);
        new FacturaManager().registrarSalida(factura);
        CobroManager cm = new CobroManager();
        DiarioManager dm = new DiarioManager();
        Cobro cobro = new Cobro();
        cobro.setFecha(new Date());
        cobro.setIdDiario(configuraciones.diario);
        cobro.setIdFactura(factura);
        cobro.setIdUsuario(configuraciones.usuario);
        cobro.setValor(CNumeros.redondear(factura.getTotal()));
        cm.crearCobro(cobro);
        configuraciones.diario.setValor(CNumeros.redondear(configuraciones.diario.getValor() + factura.getTotal()));
        dm.editarDiario(configuraciones.diario);
        txtSaldoCaja.setText(configuraciones.diario.getValor().toString());
        limpiaTodo();

    }

    private void limpiaTodo() {
        cliente = null;
        ingreso = null;
        listaDetalle = new ArrayList<Detallefactura>();
        factura = null;

        while (modelo.getRowCount() > 0) {
            modelo.removeRow(0);
        }
        txtBuscar.setText("");
        txtCedula.setText("");
        txtNombres.setText("");
        txtDescuento.setValue(0);
        txtIVA.setText("");
        txtHombres.setText("");
        txtSubtotal.setText("");
        txtSubtotalD.setText("");
        txtTotal.setText("");
        cmdCobrar.setEnabled(false);
        cmdImprimirYCobrar.setEnabled(false);
        txtBuscar.requestFocus();
    }

    private void buscarTarjeta() {
        IngresoManager ingresoManager = new IngresoManager();
        ingreso = ingresoManager.buscarCodigoIngreso(txtBuscar.getText());
        
        if (ingreso != null) {
            cliente = ingreso.getCedula();
            cmdCobrar.setEnabled(true);
            cmdImprimirYCobrar.setEnabled(true);
            factura = ingreso.getFactura();
            llenaDatos();
        } else {
            limpiaTodo();
            JOptionPane.showMessageDialog(rootPane, "No se ha registrado un ingreso para esa tarjeta");
        }
    }

    private void llenaDatos() {
        txtCedula.setText(ingreso.getCedula().getCedula());
        txtNombres.setText(ingreso.getCedula().getNombres() + " " + ingreso.getCedula().getApellidos());
        txtHombres.setText(ingreso.getNumeroPersonasH().toString());
        txtMujeres.setText(ingreso.getNumeroPersonasM().toString());
        if (factura.getDetallefacturaList() != null && factura.getDetallefacturaList().size() > 0) {
            this.listaDetalle = factura.getDetallefacturaList();

            cargaDatos();
        } else {
            if (ingreso.getNumeroPersonasH() > 0) {
                double cover = CNumeros.redondear(configuraciones.coverh / (1 + (configuraciones.iva / 100D)));
                String[] Articulo = {(ingreso.getNumeroPersonasH() - ingreso.getNumeroManillasH()) + "", "Cover Hombres", " ", cover + "", String.valueOf(CNumeros.redondear(cover * (ingreso.getNumeroPersonasH() - ingreso.getNumeroManillasH())))};
                factura.setCoverh(cover * (ingreso.getNumeroPersonasH() - ingreso.getNumeroManillasH()));
                modelo.addRow(Articulo);
            }
            if (ingreso.getNumeroPersonasM() > 0) {
                double cover = CNumeros.redondear(configuraciones.coverm / (1 + (configuraciones.iva / 100D)));
                factura.setCoverm(cover * (ingreso.getNumeroPersonasM() - ingreso.getNumeroManillasM()));
                String[] Articulo = {(ingreso.getNumeroPersonasM() - ingreso.getNumeroManillasM()) + "", "Cover Mujeres", " ", cover + "", String.valueOf(CNumeros.redondear(cover * (ingreso.getNumeroPersonasM() - ingreso.getNumeroManillasM())))};
                modelo.addRow(Articulo);
            }
            CSelecciones.producto = null;
//            txtDescuento.setValue(factura.getDescuento());
            Calcular();
        }
    }

    public void cargaDatos() {
        for (int i = 0; i < listaDetalle.size(); i++) {
            String[] Articulo = {String.valueOf(listaDetalle.get(i).getCantidad()), listaDetalle.get(i).getIdArticulo().getDescripcion(), listaDetalle.get(i).getBarra().toString(), CNumeros.redondear(listaDetalle.get(i).getPrecioVenta()) + "", String.valueOf(CNumeros.redondear(listaDetalle.get(i).getPrecioVenta() * listaDetalle.get(i).getCantidad()))};
            modelo.addRow(Articulo);
        }
        if (ingreso.getNumeroPersonasH() > 0) {
            double cover = CNumeros.redondear(configuraciones.coverh / (1 + (configuraciones.iva / 100D)));
            String[] Articulo = {(ingreso.getNumeroPersonasH() - ingreso.getNumeroManillasH()) + "", "Cover Hombres", " ", cover + "", String.valueOf(CNumeros.redondear(cover * (ingreso.getNumeroPersonasH() - ingreso.getNumeroManillasH())))};
            factura.setCoverh(cover * (ingreso.getNumeroPersonasH() - ingreso.getNumeroManillasH()));
            modelo.addRow(Articulo);
        }
        if (ingreso.getNumeroPersonasM() > 0) {
            double cover = CNumeros.redondear(configuraciones.coverm / (1 + (configuraciones.iva / 100D)));
            factura.setCoverm(cover * (ingreso.getNumeroPersonasM() - ingreso.getNumeroManillasM()));
            String[] Articulo = {(ingreso.getNumeroPersonasM() - ingreso.getNumeroManillasM()) + "", "Cover Mujeres", " ", cover + "", String.valueOf(CNumeros.redondear(cover * (ingreso.getNumeroPersonasM() - ingreso.getNumeroManillasM())))};
            modelo.addRow(Articulo);
        }
        CSelecciones.producto = null;
        txtDescuento.setValue(factura.getDescuento());
        Calcular();
    }

    private void Calcular() {

        double subtotal = 0.0D;
        double iva = 0.0D;
        double desc = Double.parseDouble(String.valueOf(this.txtDescuento.getValue()));
        double consumible = (ingreso.getNumeroPersonasH() - ingreso.getNumeroManillasH()) * configuraciones.consumibleh + (ingreso.getNumeroPersonasM() - ingreso.getNumeroManillasM()) * configuraciones.consumiblem;
        for (int i = 0; i < this.listaDetalle.size(); i++) {
            subtotal += CNumeros.redondear(((Detallefactura) this.listaDetalle.get(i)).getCantidad().doubleValue() * ((Detallefactura) this.listaDetalle.get(i)).getPrecioVenta().doubleValue());

            double sd = CNumeros.redondear(((Detallefactura) this.listaDetalle.get(i)).getCantidad().doubleValue() * ((Detallefactura) this.listaDetalle.get(i)).getPrecioVenta().doubleValue());
            sd = CNumeros.redondear(sd - sd * desc / 100.0D);
            iva += CNumeros.redondear(sd * configuraciones.iva / 100.0D);
        }
        double cover = CNumeros.redondear((ingreso.getNumeroPersonasH() - ingreso.getNumeroManillasH()) * configuraciones.coverh) + CNumeros.redondear((ingreso.getNumeroPersonasM() - ingreso.getNumeroManillasM()) * configuraciones.coverm);
        double subtd = CNumeros.redondear(subtotal - subtotal * desc / 100.0D);
        double total = CNumeros.redondear(subtd + iva);
        double ivac = CNumeros.redondear(cover * configuraciones.iva / 100.0D);
        cover = cover - ivac;
        if (consumible > total) {
            consumible = total;
        }
        total = total - consumible;
        total = total + cover + ivac;
        factura.setTotal(total);
        factura.setConsumible(consumible);
        factura.setIva(iva + ivac);
        this.txtSubtotal.setText(String.valueOf(CNumeros.redondear(subtotal + cover)));
        this.txtSubtotalD.setText(String.valueOf(CNumeros.redondear(subtd + cover)));
        this.txtIVA.setText(String.valueOf(CNumeros.redondear(iva + ivac)));
        this.txtConsumible.setText(String.valueOf(consumible));
        this.txtTotal.setText(String.valueOf(CNumeros.redondear(total)));
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdAbrir;
    private javax.swing.JButton cmdCancelar;
    private javax.swing.JButton cmdCerrarYSalir;
    private javax.swing.JButton cmdCobrar;
    private javax.swing.JButton cmdImprimirYCobrar;
    private javax.swing.JButton cmdTarjetasSinPagar;
    private javax.swing.JTable dtgFactura;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblDireccion;
    private javax.swing.JLabel lblLocal;
    private javax.swing.JLabel lblLogo;
    private javax.swing.JLabel lblPropietario;
    private javax.swing.JLabel lblTelefono;
    private javax.swing.JLabel lblUsuario;
    private javax.swing.JTextField txtBuscar;
    private javax.swing.JTextField txtCedula;
    private javax.swing.JTextField txtConsumible;
    private javax.swing.JSpinner txtDescuento;
    private javax.swing.JTextField txtHombres;
    private javax.swing.JTextField txtIVA;
    private javax.swing.JTextField txtMujeres;
    private javax.swing.JTextField txtNombres;
    private javax.swing.JTextField txtSaldoCaja;
    private javax.swing.JTextField txtSubtotal;
    private javax.swing.JTextField txtSubtotalD;
    private javax.swing.JTextField txtTotal;
    // End of variables declaration//GEN-END:variables
}
